<?php
namespace Rootadmin\Controller;
use Think\Controller;
class LoginController extends Controller{
    /**
     * 登录
     */
	public function index(){
	    if(IS_POST){

            $name = I('name', '');
            $password = I('password', '', 'md5');
            if(empty($name)||empty($password)){
                $this->ajaxReturn(array('success'=>false, 'msg'=>'请输入帐号密码'));
            }
            $admin = M("admin")->where(array('name'=>$name))->find();
            if($admin){
                if($admin['password'] != $password){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'账号或密码不正确'));
                }else{

                    session('userid', $admin['id']);
                    session('name', $admin['name']);

                    $this->ajaxReturn(array('success'=>true, 'msg'=>'登录成功'));
                }
            }else{
                $this->ajaxReturn(array('success'=>false, 'msg'=>'账号或密码不正确'));
            }
        }else{
	        $this->display();
        }
	}

    /**
     * 退出登录
     */
	public function logout(){
		session('userid', NULL);
		session('name', NULL);
		$this->redirect('Rootadmin/Login/index');
	}
    /**
     * 修改密码
     */
	public function changePwd(){
        $userid = session("userid");
        $name = session("name");
        if(empty($userid) || empty($name)){
            header("location:/Rootadmin/login/index");
        }
	    if(IS_POST){
            $password = I('pwd', '', 'md5');
            $newpassword = I('newpwd', '', 'md5');
            if(!empty($userid)){
                $nowpassword = M("admin")->where(array('id'=>$userid))->getField('password');
                if($nowpassword != $password){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'原密码不正确'));
                }
                $rs = M("admin")->where(array('id'=>$userid))->setField('password', $newpassword);
                if($rs){
                    $this->ajaxReturn(array('success'=>true, 'msg'=>'修改成功'));
                }
            }else{
                $this->redirect("Rootadmin/Login/index");
            }
        }else{
            $this->display();
        }
	}
	//查看商户信息
    public function info(){
        $shopid = session("shopid");
        $shopname = session("shopname");
        if(empty($shopid) || empty($shopname)){
            header("location:/Shopadmin/login/index");
        }
	    $info=M("shop")->where("id=$shopid")->find();
	    $this->assign("shopname",session("shopname"));
	    $this->assign("info",$info);
	    $this->display();
    }
}